package com.ls.fw.mvc.springmvc.support.hateoas.utils.link;

import java.net.URI;
import java.net.URISyntaxException;

import org.springframework.hateoas.Link;

/**
 * A generic interface for providing links to rest resources, with sensible reuse of defined methods.
 *
 * @param <R> the resource type that this link factory relates to (normally should be hard coded in implementing class)
 * @author Andrew Hill
 */
public abstract class AbstractResourceLinkFactory<R> implements ResourceLinkFactory<R> {

    public Link getLinkForIdentifier(String relation, String identifier) {
        return getLinkForIdentifier(identifier).withRel(relation);
    }

    public URI getUriForIdentifier(String identifier) {
        Link self = getLinkForIdentifier(identifier);

        URI uri = null;
        try {
            uri = new URI(self.getHref());
        } catch (URISyntaxException e) {}

        return uri;
    }

    public URI getUriForEntity(R entity) {
        String identifier = getIdentifierForEntity(entity);
        return getUriForIdentifier(identifier);
    }
}
